package com.googlecode.jpocketmod.actions;

import com.googlecode.jpocketmod.DialogManager;
import com.googlecode.jpocketmod.state.AppState;
import com.googlecode.jpocketmod.templates.PocketModTemplate;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.Action;

/**
 *
 * @author David Sosby
 */
public class SaveTemplateAction extends ApplicationAction  {
	public SaveTemplateAction()  {
		this.putValue(Action.NAME, "Save Template");
		this.putValue(Action.SHORT_DESCRIPTION, "Save a JPocketMod Template");
		this.putValue(Action.LONG_DESCRIPTION, "Save a JPocketMod Template");
		this.putValue(Action.ACTION_COMMAND_KEY, "s");
	}

	public void actionPerformed(ActionEvent e) {
		runAction();
	}

	@Override
	protected void runAction() {
		File fileToSaveTo = DialogManager.getInstance().showSaveTemplateDialog();
		System.out.println("Saving template: " + fileToSaveTo);
		
		try  {
			PocketModTemplate.saveToFile(AppState.getInstance().getCurrentlyLoadedTemplate(),
								 fileToSaveTo);
		}
		catch (Exception ex)  {
			System.out.println("Exception saving: " + ex);
			//TODO: Display dialog showing error message
		}
	}
}
